﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ww.form.wwf;
using ww.lis.lisbll.sam;
using ww.wwf.wwfbll;
using System.Collections;
namespace ww.form.lis.sam.sq
{
    public partial class SamplingFormSelect : SysBaseForm
    {
        ApplyBLL bllApply = new ApplyBLL();//申请规则
        DeptBll deptbll = new DeptBll();
        DataTable dtMain = new DataTable();
        DataTable dtResult = new DataTable();
        //private ww.wwf.wwfbll.UserdefinedFieldUpdateForm gridMain = new ww.wwf.wwfbll.UserdefinedFieldUpdateForm("6");
        //private ww.wwf.wwfbll.UserdefinedFieldUpdateForm gridResult = new ww.wwf.wwfbll.UserdefinedFieldUpdateForm("7");
        string str_fsample_id = "";
        string str_fapply_id = "";
        string str_fstate = "";

        public SamplingFormSelect()
        {
            InitializeComponent();
            this.dataGridViewItem.AutoGenerateColumns = false;
        }

        private void SamplingBarCodeForm_Load(object sender, EventArgs e)
        {
            try
            {
                this.ActiveControl = this.tbfsample_barcode;
                this.cmb_dept.DataSource = deptbll.BllDeptDTuse(); //申请部门
                this.cmb_dept.DisplayMember = "fname";
                this.cmb_dept.ValueMember = "fdept_id";
                this.cmb_dept.SelectedIndex = -1;

                fjy_dateDateTimePicker1.Value = Convert.ToDateTime(bllApply.DbSysTime());
                fjy_dateDateTimePicker2.Value = Convert.ToDateTime(bllApply.DbSysTime());

                //dtMain = this.bllApply.BllSamplingMainDT(" t2.fsample_barcode='-1'");
                //dataGridViewApply.DataSource = dtMain;

                //this.dtResult = this.bllApply.BllSamplingResultDT("-1");
                //this.dataGridViewItem.DataSource = this.dtResult;

                //this.gridMain.DataGridViewSetStyle(this.dataGridViewApply);
                //this.gridResult.DataGridViewSetStyle(this.dataGridViewItem);

                bt_find_Click(null, null);
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        #region 方法
        private void GetMailList()
        {
            this.bindingSource1.DataSource = this.bllApply.BllSamplingMainDT("t2.fsample_barcode='" + this.tbfsample_barcode.Text + "'");
            this.dataGridViewApply.DataSource = this.bindingSource1;
            this.tbfsample_barcode.Text = "";
            this.ActiveControl = this.tbfsample_barcode;
        }
        private void GetResultList(string fapply_id)
        {
            this.dtResult = this.bllApply.BllSamplingResultDT(fapply_id);
            this.dataGridViewItem.DataSource = this.dtResult;
        }
        #endregion

        private void tbfsample_barcode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//或(e.KeyCode==Keys.Enter) 
            {
                try
                {
                    Cursor = Cursors.WaitCursor;
                    GetMailList();
                }
                catch (Exception ex)
                {
                    WWMessage.MessageShowError(ex.ToString());
                }
                finally
                {
                    Cursor = Cursors.Arrow;
                }
            }
        }

        private void bindingSource1_PositionChanged(object sender, EventArgs e)
        {
            try
            {
                DataRowView rowCurrent = (DataRowView)this.bindingSource1.Current;
                if (this.dtResult.Rows.Count > 0)
                    this.dtResult.Clear();
                if (rowCurrent != null)
                {
                    //str_fsample_id = rowCurrent["fsample_id"].ToString();
                    str_fapply_id = rowCurrent["fapply_id"].ToString();
                    //str_fstate = rowCurrent["fstate"].ToString();
                    GetResultList(str_fapply_id);
                }

            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }         

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            try
            {
                HelpForm help = new HelpForm("c629e6a854134cb2a24fc6465bc35b77");
                help.Show();
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void bt_find_Click(object sender, EventArgs e)
        {
            try
            {
                string strDateif = " (t1.fapply_time>='" + fjy_dateDateTimePicker1.Value.ToString("yyyy-MM-dd") + " 00:00:00" + "' and t1.fapply_time<='" + fjy_dateDateTimePicker2.Value.ToString("yyyy-MM-dd") + " 24:00:00" + "')";

                string fhz_dept = "";//病人部门
                if (object.Equals(cmb_dept.SelectedValue, null))//病人部门
                    fhz_dept = "";
                else
                    fhz_dept = cmb_dept.SelectedValue.ToString();
                if (fhz_dept == "" || fhz_dept == null)
                    fhz_dept = "";
                else
                    fhz_dept = " and (t1.fapply_dept_id='" + fhz_dept + "')";

                dtMain = this.bllApply.BllSamplingMainDT(strDateif + fhz_dept);
                this.bindingSource1.DataSource = dtMain;
                dataGridViewApply.DataSource = bindingSource1;

                for (int intGrid = 0; intGrid < this.dataGridViewApply.Rows.Count; intGrid++)
                {
                    string fstate = this.dataGridViewApply.Rows[intGrid].Cells["fstate"].Value.ToString().Trim();
                    if (fstate == "1")
                        this.dataGridViewApply.Rows[intGrid].DefaultCellStyle.ForeColor = Color.Black; //System.Drawing.
                    else if (fstate == "2")
                        this.dataGridViewApply.Rows[intGrid].DefaultCellStyle.ForeColor = Color.Blue; //System.Drawing.
                    else if (fstate == "3")
                        this.dataGridViewApply.Rows[intGrid].DefaultCellStyle.ForeColor = Color.Red; //System.Drawing.
                    else
                        this.dataGridViewApply.Rows[intGrid].DefaultCellStyle.ForeColor = Color.Black; //System.Drawing.
                }
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void 取消采样toolStripButton_Click(object sender, EventArgs e)
        {

            if (str_fstate != "2")
            {
                WWMessage.MessageShowError("样本状态不允许取消采集，请检查!");
                return;
            }
            this.bllApply.BllSamplingCancel_fsampling_time(str_fsample_id);
            int Position = bindingSource1.Position;
            bt_find_Click(null, null);
            bindingSource1.Position = Position;
        }

        private void 取消签收toolStripButton_Click(object sender, EventArgs e)
        {
            if (str_fstate != "3")
            {
                WWMessage.MessageShowError("样本状态不允许取消签收，请检查!");
                return;
            }
            this.bllApply.BllSamplingCancel_fget_user_time(str_fsample_id);
            int Position = bindingSource1.Position;
            bt_find_Click(null, null);
            bindingSource1.Position = Position;
        }

        private void dataGridViewApply_SelectionChanged(object sender, EventArgs e)
        {
            if (object.Equals(dataGridViewApply.CurrentRow, null)) return;
            str_fstate = dataGridViewApply.CurrentRow.Cells["fstate"].Value.ToString().Trim();
            str_fsample_id = dataGridViewApply.CurrentRow.Cells["fsample_id"].Value.ToString().Trim(); 
        }       
       
    }
}